package com.apusic.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apusic.common.PageInfo;
import com.apusic.entity.BaseResource;
import com.apusic.entity.TBaseDistrict;
import com.apusic.entity.TLog;
import com.apusic.service.BaseResourceService;
import com.apusic.service.DistrictService;
import com.apusic.service.LogService;
import com.apusic.util.DateUtil;
import com.apusic.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;


@Controller
@RequestMapping(value = "/log")
public class LogController {
	
	@Autowired
	private LogService logService;
	
	@Autowired
	private DistrictService districtService;
	
	@Autowired
	private BaseResourceService baseResourceService;

	@RequestMapping(value = "/toLog")
	public String toLog(HttpServletRequest request,
			HttpServletResponse response) {
		
		List<TBaseDistrict> districts = districtService.getDistricts();
		request.setAttribute("districts", districts);
		
		List<BaseResource> resources = baseResourceService.getResources();
		request.setAttribute("resources", resources);
		
		return "log/logList";
	}
	
	/**
	 * 日志分析页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toLogStat")
	public String toLogStat(HttpServletRequest request,
			HttpServletResponse response) {
		
		List<TBaseDistrict> districts = districtService.getDistricts();
		request.setAttribute("districts", districts);
		
		List<BaseResource> resources = baseResourceService.getResources();
		request.setAttribute("resources", resources);
		
		return "log/logStat";
	}
	
	/**
	 * 日志访问趋势
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toLogTrend")
	public String toLogTrend(HttpServletRequest request,
			HttpServletResponse response) {
		
		List<TBaseDistrict> districts = districtService.getDistricts();
		request.setAttribute("districts", districts);
		
		return "log/logTrend";
	}
	
	
	@RequestMapping(value = "/getLogData")
	@ResponseBody
	public String getLogData(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(name="pageNum",defaultValue="0") int pageNum,
			@RequestParam(name="pageSize",defaultValue="20") int pageSize) {
		
		
		String district = request.getParameter("district");
		String module = request.getParameter("module");
		String accessUser = request.getParameter("accessUser");
		
		List<TBaseDistrict> districts = districtService.getDistricts();
		Map<String,String> dmap = new HashMap<String,String>();
		for(TBaseDistrict dt : districts){
			dmap.put(dt.getCode(), dt.getName());
		}
		
		List<BaseResource> resources = baseResourceService.getResources();
		Map<String,String> rmap = new HashMap<String,String>();
		for(BaseResource resource : resources){
			rmap.put(resource.getResourceUrl(), resource.getResourceName());
		}
		
		PageHelper.startPage(pageNum, pageSize);
		Map<String,Object> map = new HashMap<String,Object>();
		
		if(district!=null && !("").equals(district)){
			map.put("district", district);
		}
		
		if(module!=null && !("").equals(module)){
			map.put("module", module);
		}
		
		if(accessUser!=null && !("").equals(accessUser)){
			map.put("accessUser", accessUser);
		}
		
		List<TLog> logList = logService.getLogData(map);
		for(TLog log : logList){
			String dt = log.getAccessDistrict();
			log.setDistrictName(dmap.get(dt));
			String md = log.getModule();
			log.setModuleName(rmap.get(md));
		}
		
		
		PageInfo<TLog> pageInfo = new PageInfo<TLog>(logList);
		String str = JsonUtils.objectToJson(pageInfo);
		return str;
		
	}
	
	@RequestMapping(value = "/getLogStat")
	@ResponseBody
	public String getLogStat(HttpServletRequest request, HttpServletResponse response) {
		

		String district = request.getParameter("district");
		String accessDate = request.getParameter("accessDate");
		
		if(district==null || district.equals("100000")){
			district = "";
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		if(district!=null && !("").equals(district)){
			map.put("district", district);
		}
		
		if(accessDate!=null && !("").equals(accessDate)){
			map.put("accessDate", accessDate+"%");
		}
		TLog pvLog = logService.getLogPv(map);
		
		List<TLog> pvLogs = logService.getLogModulePV(map);
		
		Map<String,TLog> logMap = new HashMap<String,TLog>();
		for(TLog log : pvLogs){
			logMap.put(log.getModule(),log);
		}
		
		List<TLog> pvLogVos = new ArrayList<TLog>();
		
		List<BaseResource> resources = baseResourceService.getResources();
		for(BaseResource r : resources){
			if(logMap.containsKey(r.getResourceUrl())){
				pvLogVos.add(logMap.get(r.getResourceUrl()));
			}else{
				TLog log = new TLog();
				log.setModuleName(r.getResourceName());
				log.setPvCount("0");
				pvLogVos.add(log);
			}
		}
		
		
		String result = pvLog.getPvCount()+"|"+JsonUtils.objectToJson(pvLogVos);
		
		return result;
	}
	
	
	@RequestMapping(value = "/getLogTrend")
	@ResponseBody
	public String getLogTrend(HttpServletRequest request, HttpServletResponse response) {

		String district = request.getParameter("district");
		
		if(district==null || district.equals("100000")){
			district = "";
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		if(district!=null && !("").equals(district)){
			map.put("district", district);
		}
		
		List<TLog> pvLogs = logService.getLogTrendPV(map);
		
		Map<String,TLog> logMap = new HashMap<String,TLog>();
		for(TLog log : pvLogs){
			logMap.put(log.getAccessTime(),log);
		}
		
		List<TLog> pvLogVos = new ArrayList<TLog>();
		
		List<String> dayList = DateUtil.getLastThirtyDays();
		for(String day : dayList){
			if(logMap.containsKey(day)){
				pvLogVos.add(logMap.get(day));
			}else{
				TLog log = new TLog();
				log.setAccessTime(day);
				log.setPvCount("0");
				pvLogVos.add(log);
			}
		}
		
		String result = JsonUtils.objectToJson(pvLogVos);
		
		return result;
	}
}
